﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;
using TracNghiemTinHocDaiCuong.BIZ;

namespace TracNghiemTinHocDaiCuong
{
    public partial class SuaNguoiDung : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NguoiDung nguoiDung = TimNguoiDung();
            if (!IsPostBack)
            {
                txtTenTaiKhoan.Text = nguoiDung.TenTaiKhoan;
                txtMatKhau.Text = nguoiDung.MatKhau;
                txtHoTen.Text = nguoiDung.HoVaTen;
                txtNgaySinh.Text = nguoiDung.NgaySinh.ToString();
                txtEmail.Text = nguoiDung.Email;
                if (nguoiDung.GioiTinh == "Nam")
                    rblGioiTinh.SelectedIndex = 0;
                else
                    rblGioiTinh.SelectedIndex = 1;
                txtDienThoai.Text = nguoiDung.DienThoai;
                txtDiaChi.Text = nguoiDung.DiaChi;
                txtNgayDangKi.Text = nguoiDung.NgayDangKi.ToString();
            }
        }

        private NguoiDung TimNguoiDung()
        {
            ViewState["idNguoiDung"] = Methods.ConvertNumber(Request.QueryString["ID"]);
            NguoiDung nguoiDung = NguoiDungBIZ.TimKiemTheoId((int)ViewState["idNguoiDung"]);
            return nguoiDung;
        }

        protected void btnSuaNguoiDung_Click(object sender, EventArgs e)
        {
            NguoiDung NguoiDungSua = new NguoiDung();
            NguoiDung NguoiDungCu = TimNguoiDung();
            NguoiDungSua.MaND = (int)ViewState["idNguoiDung"];
            NguoiDungSua.TenTaiKhoan = txtTenTaiKhoan.Text;
            NguoiDungSua.MatKhau = txtMatKhau.Text;
            NguoiDungSua.HoVaTen = txtHoTen.Text;
            NguoiDungSua.NgaySinh = Methods.ConvertDateTime(ConvertDateTimeToString(txtNgaySinh.Text));
            NguoiDungSua.Email = txtEmail.Text;
            NguoiDungSua.GioiTinh = rblGioiTinh.SelectedValue;
            NguoiDungSua.DienThoai = txtDienThoai.Text;
            NguoiDungSua.DiaChi = txtDiaChi.Text;
            NguoiDungSua.NgayDangKi = Methods.ConvertDateTime(txtNgayDangKi.Text);
            NguoiDungBIZ.Sua(NguoiDungSua);
            lblThongBao.Text = "Bạn đã sửa " + NguoiDungCu.TenTaiKhoan + " thành công";
        }

        private string ConvertDateTimeToString(string exp)
        {
            if (string.IsNullOrEmpty(exp))
                return "04/01/2013";
            string[] time = exp.Split('/');
            if (Methods.ConvertNumber(time[0]) > 23)
                return "04/01/2013";
            if (Methods.ConvertNumber(time[1]) > 59)
                return "04/01/2013";
            return string.Format("{0}/{1}/{2}",
                time[1], time[0], time[2]);
        }


        protected void btnQuayLai_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuanLyNguoiDung.aspx");
        }
    }
}